package fun.lifepoem.blog.jobs;

import fun.lifepoem.blog.domain.CommitRecord;
import fun.lifepoem.blog.mapper.CommitRecordMapper;
import fun.lifepoem.blog.service.GitService;
import fun.lifepoem.blog.service.GiteeFallbackService;
import fun.lifepoem.blog.utils.GitUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author Yiwyn
 * @create 2022/9/15 23:45
 * @desc 定时任务 ，用来处理没有用处的tag
 */
@Component
@Slf4j
public class GitArticleJob {

    @Autowired
    private GitUtils gitUtils;
    @Autowired
    private GitService gitService;

    @Autowired
    private CommitRecordMapper commitRecordMapper;

    @Autowired
    private GiteeFallbackService giteeFallbackService;

    @Scheduled(cron = "0 0 12 * * ? ")
    public void pullNote() throws Exception {
        gitUtils.pullRepository();
    }

    @Scheduled(cron = "0 0 1 * * ? ")
    public void getGitLog() throws Exception {
        gitService.recordCommits();
    }

    @Scheduled(cron = "0 0 2 * * ? ")
    public void updateArticle() throws Exception {
        List<CommitRecord> records = commitRecordMapper.selectByCommitRecord(new CommitRecord() {{
            setUseFlag(false);
        }});
        records = Optional.ofNullable(records).orElse(new ArrayList<>());
        giteeFallbackService.giteePushFallback(records);
    }


}
